#include <stdio.h>
#include <e_file.h>
#include <string.h>

#define BUFFSIZE 32
int mycopy (int rfd, int wfd);

int main (int argc, char ** argv)
{
	int fd1, fd2;
	int cnt = 0;
	char buf[BUFFSIZE] = {0};
	if (argc < 3) {
		return -1;
	}
	//打开argv[1]文件
	fd1 = open (argv[1], O_RDONLY);
	if (-1 == fd1) {
		fprintf (stderr, "open() failed!\n");
	}
	//打开argv[2]文件
	fd2 = open (argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);
	if (-1 == fd2) {
		fprintf (stderr, "open() failed!\n");
	}
	//改变文件的偏移位置
	printf ("当前文件的偏移位置:%ld\n", lseek (fd1, 7, SEEK_SET));
	mycopy (fd1, fd2);
	close (fd1);
	close (fd2);
	return 0;
}

int mycopy (int rfd, int wfd)
{
	int cnt = 0;
	char buf[BUFFSIZE] = {0};
	while (1) {
		memset (buf, 0, BUFFSIZE);
		cnt = read (rfd, buf, BUFFSIZE);
		if (-1 == cnt) {
			fprintf (stderr, "read() failed!\n");
			goto ERROR;
		}
		if (0 == cnt) {
			break;
		}
		write (wfd, buf, cnt);
	}
	return 0;
ERROR:
	close (rfd);
	close (wfd);
	return -1;
}






